package com.syh.jn.scm.infrastructure.repository.impl.supplier.finance;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.supplier.entity.finance.PurchaseInvoice;
import com.syh.jn.scm.domain.supplier.repository.finance.PurchaseInvoiceRepository;
import com.syh.jn.scm.dto.supplier.finance.PurchaseInvoiceVo;
import com.syh.jn.scm.infrastructure.repository.model.finance.InvoiceModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class PurchaseInvoiceRepositoryImpl extends AbstractAggregateRepository<PurchaseInvoice, InvoiceModel, Long> implements PurchaseInvoiceRepository {
  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String, String> map = new HashMap<>();
    map.put("id", "salesInvoice.ID");
    map.put("statementOfAccountId", "salesInvoice.STATEMENT_OF_ACCOUNT_ID");
    map.put("statementOfAccountCode","statementAccount.CODE");
    map.put("state", "salesInvoice.STATE");
    map.put("expressListCode", "salesInvoice.EXPRESS_LIST_CODE");
    map.put("ourInvoiceCompanyName", "salesInvoice.OUR_INVOICE_COMPANY_NAME");
    map.put("ourInvoiceTaxpayerCode", "salesInvoice.OUR_INVOICE_TAXPAYER_CODE");
    map.put("otherPartyInvoiceCompanyName", "salesInvoice.OTHER_PARTY_INVOICE_COMPANY_NAME");
    map.put("otherPartyInvoiceTaxpayerCode", "salesInvoice.OTHER_PARTY_INVOICE_TAXPAYER_CODE");
    map.put("billingTimestamp", "salesInvoice.BILLING_TIMESTAMP");
    map.put("invoiceType", "salesInvoice.INVOICE_TYPE");
    map.put("signForCompanyId", "salesInvoice.SIGN_FOR_COMPANY_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return map.get(propertyName);
    });
    return parse;
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(SQLAliasGenerateUtil.aliasGenerate(InvoiceModel.class, "salesInvoice", null),
              "statementAccount.CODE AS statementOfAccountCode",
              "statementAccount.PRICE AS statementOfAccountPrice");
      FROM("T_FINANCE_INVOICE AS salesInvoice");
      INNER_JOIN("T_FINANCE_STATEMENT_ACCOUNT_BILL AS statementAccount ON statementAccount.id = salesInvoice.STATEMENT_OF_ACCOUNT_ID");
    }};
  }

  @Override
  protected Class getViewObject() {
    return PurchaseInvoiceVo.class;
  }

}
